﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;

namespace KeyRemapper
{
    public class Program : Form
    {
        public static Program Instance { get; private set; }

        public KeyboardHook KeyboardHook { get; private set; }
        public Remapper Remapper { get; private set; }
        public Hotkeys Hotkeys { get; private set; }

        public NotifyIcon NotifyIcon { get; private set; }
        public frmSettings Settings { get; private set; }
        
        private Program()
        {
            base.WindowState = FormWindowState.Minimized;
            base.FormClosing += Program_FormClosing;

            KeyboardHook = new KeyboardHook();
            Hotkeys = new Hotkeys(KeyboardHook)
            {
                Enabled = true,
                HotkeyList = new List<Hotkey>()
                {
                    new Hotkey()
                    {
                        Key = Keys.P,
                        Ctrl = true,
                        Shift = true
                    }
                }
            };
            Remapper = new Remapper(KeyboardHook)
                {
                    Enabled = false,
                };

            Hotkeys.OnHotkeyPressed += Hotkeys_OnHotkeyPressed;

            try
            {
                KeyboardHook.Hook();
            }
            catch
            {
                MessageBox.Show("Fatal error occurred, unable to set keyboar hook. Program will exit!", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }

            Settings = new frmSettings();

            NotifyIcon = new NotifyIcon();
            NotifyIcon.Text = "Key Remapper";
            NotifyIcon.Icon = Properties.Resources.Tpdkdesign_net_Refresh_Cl_Hardware_Keyboard;
            NotifyIcon.DoubleClick += NotifyIcon_DoubleClick;

            NotifyIcon.Visible = true;
        }

        private void NotifyIcon_DoubleClick(object sender, EventArgs e)
        {
            Settings.Show();
        }

        private void Program_FormClosing(object sender, FormClosingEventArgs e)
        {
            KeyboardHook.Unhook();
        }

        private void Hotkeys_OnHotkeyPressed(Hotkey obj)
        {
            if (obj.Key == Keys.P)
            {
                if (!Remapper.Enabled)
                    Remapper.Update(Properties.Settings.Default.UseDynamicKeymap, Properties.Settings.Default.Seed);
                Remapper.Enabled = !Remapper.Enabled;

                base.BeginInvoke(new MethodInvoker(() => NotifyIcon.ShowBalloonTip(3000, "Key Remapper", Remapper.Enabled ? "Enabled" : "Disabled", ToolTipIcon.Info)));
            }
        }

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            base.Hide();
        }

        public static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(Instance = new Program());
        }
    }
}
